<?php
/**
 * Created by PhpStorm.
 * User: luxuewen_gz
 * Date: 2019/1/3
 * Time: 14:47
 */

namespace app\admin\logic;


use think\Session;

class Access
{
    public function accessCheck($controller, $method){
        // 不需要受权限控制的页面
        $ignorePage = [
            'Index/main'
        ];
        if (in_array($controller."/".$method, $ignorePage)){
            return true;
        }
        $userInfo = (new Session())->get('USER_INFO');
        $urWhere[] = ['is_delete', 'eq', 0];
        $urWhere[] = ['user_id', 'eq', $userInfo['userId']];
        $roleIds = array_unique(array_column(model('UserRole')->where($urWhere)->field('role_id')->select()->toArray(), 'role_id'));
        $rpWhere[] = ['is_delete', 'eq', 0];
        $rpWhere[] = ['role_id', 'in', $roleIds];
        $permissionIds = array_unique(array_column(model('RolePermission')->where($rpWhere)->field('permission_id')->select()->toArray(), 'permission_id'));
        $pmWhere[] = ['is_delete', 'eq', 0];
        $pmWhere[] = ['permission_id', 'in', $permissionIds];
        $menuIds = array_unique(array_column(model('PermissionMenu')->where($pmWhere)->field('menu_id')->select()->toArray(), 'menu_id'));
        $mWhere[] = ['is_delete', 'eq', 0];
        $mWhere[] = ['id', 'in', $menuIds];
        $mWhere[] = ['controller', 'eq', $controller];
        $mWhere[] = ['method', 'eq', $method];
        $menu = model('Menu')->where($mWhere)->fetchSql(false)->find();
        return $menu ? true : false;
    }
}